#include "UIScreen.h"
#include <tchar.h>

UIScreen::UIScreen(UIControl* p_pParent) : UIControl(p_pParent)
{
}

UIScreen::~UIScreen()
{
	for (list<UIControl* >::iterator it = m_pControls.begin(); it != m_pControls.end(); it++)
	{
		delete *it;
	}
	m_pControls.clear();
}
void UIScreen::drawControls()
{
	
}
void UIScreen::draw()
{
	m_pFocusing = 0;
	for (list<UIControl* >::iterator it = m_pControls.begin(); it != m_pControls.end(); it++)
	{
		if ((*it)->isFocus())
		{
			m_pFocusing = *it;
		}
		else
		{
			(*it)->draw();
		}
	}
	if (m_pFocusing)
	{
		m_pFocusing->draw();
	}
}

void UIScreen::onLostFocus()
{
	m_pFocusing->onLostFocus();
	m_pFocusing = 0;
	UIControl::onLostFocus();
}

void UIScreen::add(UIControl* p_pControl)
{
	if(p_pControl->getParent() && _tcscmp(p_pControl->getParent()->getType(), TEXT("UIScreen")) == 0)
	{
		((UIScreen*)(p_pControl->getParent()))->remove(p_pControl);
	}
	p_pControl->setParent(this);
	m_pControls.push_back(p_pControl);
}

LPSTR UIScreen::getType()
{
	return TEXT("UIScreen");
}

void UIScreen::remove(UIControl* p_pControl)
{
	m_pControls.remove(p_pControl);
	p_pControl->setParent(NULL);
}

LRESULT UIScreen::msgProc(HWND hWnd, UINT p_uMsg, WPARAM p_wParam, LPARAM p_lParam)
{
	switch (p_uMsg)
	{
	case WM_LBUTTONDOWN:
	case WM_RBUTTONDOWN:
	case WM_MBUTTONDOWN:
		{
			MouseEvent me(this, p_uMsg, p_wParam, p_lParam);
			onMouseDown(&me);
			if (me.isHandled())
			{
				return 0;
			}
		}
		break;
	case WM_LBUTTONUP:
	case WM_RBUTTONUP:
	case WM_MBUTTONUP:
		{
			MouseEvent me(this, p_uMsg, p_wParam, p_lParam);
			onMouseUp(&me);
			if (me.isHandled())
			{
				return 0;
			}
		}
		break;
	case WM_MOUSEMOVE:
		{
			MouseEvent me(this, p_uMsg, p_wParam, p_lParam);
			onMouseMove(&me);
			if (me.isHandled())
			{
				return 0;
			}
		}
		break;	
	}	
	return DefWindowProc(hWnd, p_uMsg, p_wParam, p_lParam);
}

void UIScreen::onMouseUp(MouseEvent* p_event)
{

}

void UIScreen::onHotKey()
{

}
void UIScreen::update( float p_fDt )
{

}

void UIScreen::onKeyUp()
{

}

void UIScreen::onKeyDown()
{

}

void UIScreen::onMouseDown(MouseEvent* p_event)
{

}

void UIScreen::onMouseMove(MouseEvent* p_event)
{

}

void UIScreen::onMouseEnter(MouseEvent* p_event)
{

}

void UIScreen::onMouseOut(MouseEvent* p_event)
{

}